const mysql = require('mysql')
const db = mysql.createPool({
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  password: '123456',
  database: 'my_db'
})

/* db.query('select * from users', (err, result) => {
  if (err) {
    console.log(err.message);
  } else {
    console.log(result);
  }
}) */

/* const user = {
  username: 'spider man2',
  password: 'pcc4321'
}
const sql = 'insert into users (username,password) values (?,?)'
db.query(sql, [user.username, user.password], (err, result) => {
  if (err) {
    return console.log(err.message);
  }
  if (result.affectedRows === 1) {
    console.log('添加数据成功');
  }
}) */

/* const user = {
  id: 15,
  username: 'batman',
  password: '000'
}
const sql = 'update users set username=?,password=? where id=?'
db.query(sql, [user.username, user.password, user.id], (err, result) => {
  if (err) {
    return console.log(err.message);
  }
  if (result.affectedRows === 1) {
    console.log('数据更新成果');
  }
}) */

const sql = 'delete from users where id=?'
db.query(sql, 12, (err, result) => {
  if (err) {
    return console.log(err.message);
  }
  if (result.affectedRows === 1) {
    console.log('数据删除成功');
  }
})